package edu.hut.bookshop.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import com.github.pagehelper.Page;
import edu.hut.bookshop.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import edu.hut.bookshop.dao.BookMapper;
import edu.hut.bookshop.pojo.Book;
import edu.hut.bookshop.service.BookService;

@Service
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;

    @Autowired
	private RedisUtil redisUtil;

    @Resource
	private RedisTemplate redisTemplate;

	@Override
	@Cacheable(cacheNames = "jiajuserchById",unless = "#result==null")
	public Book bookSearchById(Integer bookId) {
		// TODO Auto-generated method stub
		Book books=bookMapper.selectByBookId(bookId);
		//redisUtil.hset("jiajus","jiajus",books);
		//System.out.println("现在有测试到这个");

		return books;
	}

	@Override
	@Cacheable(cacheNames = "jiajusearchByname",unless = "#result==null")
	public List<Book> bookSearchBybName(String bName,Integer page,Integer limit) {
		PageHelper.startPage(page, limit);
		List<Book> books=bookMapper.selectBybName(bName);
		return books;
	}

	//(修改成分页查询)
	@Override
	@Cacheable(cacheNames = "jiajuSearchByCode",unless = "#result==null")
	public List<Book> bookSearchByCode(String catrgoryCode,Integer page,Integer limit) {
			PageHelper.startPage(page, limit);
	      List<Book> books = bookMapper.selectAllByCategoryCode(catrgoryCode);
	   
	      return books;
		// TODO Auto-generated method stub

	}

	@Override
	@CacheEvict(cacheNames = "deljiaju")
	public int bookDeleteSearchById(Integer bookId) {
		// TODO Auto-generated method stub
		int books=bookMapper.deleteByBookId(bookId);
		//redisTemplate.delete()
		return books;
	}

	@Override

	public int bookInsert(Book record) {
		// TODO Auto-generated method stub
		int books=bookMapper.insert(record);
		redisTemplate.opsForValue().set("jiaju_"+record.getBookId(),record,1, TimeUnit.MINUTES );
                return books;
	}

	@Override
	@CachePut(cacheNames = "jiajuUpdate",unless = "#result==null")
	public int bookUpdate(Book record) {
		// TODO Auto-generated method stub
		int books=bookMapper.updateByBookId(record);
		return books;

	}


	/**
	 * 多条件查询   ----by guozongchao
	 * @param book
	 * @return
	 */
	@Override
	public List<Book> searchBooks(Book book, Integer page, Integer limit) {
		PageHelper.startPage(page,limit);
		List<Book> books = bookMapper.selectByBooks(book);
		return books;
	}
}
